package com.vip.fdfs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vip.common.constant.SysConsts;
import com.vip.common.response.ErrorCode;
import com.vip.common.response.Result;
import com.vip.common.response.Results;
import com.vip.common.util.EmptyUtils;
import com.vip.fdfs.constant.FdfsConsts;
import com.vip.fdfs.dto.GetFileDTO;
import com.vip.fdfs.entity.SysFastdfsFile;
import com.vip.fdfs.mapper.SysFastdfsFileMapper;
import com.vip.fdfs.service.IFileBaseInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 文件数据库操作Service
 *
 * @author wgb
 * @date 2020/4/23 19:17
 */
@Slf4j
@Service
public class FileBaseInfoServiceImpl implements IFileBaseInfoService {

    @Autowired
    private SysFastdfsFileMapper fastdfsFileMapper;
    @Value("${ext-fdfs-ip}")
    private String extFastDfsIp;

    @Override
    public Result<List<GetFileDTO>> getFileInfoList(Integer pageNo, Integer pageSize, String fileName, Long startTime, Long endTime, Boolean isDel) {
        try{
            PageHelper.startPage(pageNo, pageSize);
            List<GetFileDTO> result = fastdfsFileMapper.selectFileList(EmptyUtils.removeBasicEmpty(fileName)
                  , EmptyUtils.basicIsEmpty(startTime) ? null : new Date(startTime), EmptyUtils.basicIsEmpty(endTime) ? null : new Date(endTime)
                  , EmptyUtils.basicIsEmpty(isDel) ? null : isDel);
            PageInfo<GetFileDTO> pageInfo = new PageInfo<>(result);
            // 拼接文件路径：如果是图片则返回预览路径，如果是MP4文件则直接返回播放路径，否则返回下载路径
            for (GetFileDTO file : pageInfo.getList()) {
                if (SysConsts.IMAGE_TYPE.contains(file.getSuffix())) {
                    file.setPath(FdfsConsts.IMG_VIEW_BASE_URL + file.getId()).setThumbImagePath(extFastDfsIp + file.getThumbImagePath());
                } else if(FdfsConsts.VIDEO_PLAY_TYPE.equalsIgnoreCase(file.getSuffix())){
                    file.setPath(extFastDfsIp + file.getPath()).setThumbImagePath(extFastDfsIp + file.getThumbImagePath());
                }else {
                    file.setPath(String.format("%s%s?isSource=true", FdfsConsts.FILE_DOWN_BASE_URL, file.getId()))
                          .setThumbImagePath(FdfsConsts.VIDEO_TYPE.contains(file.getSuffix()) ? extFastDfsIp + file.getThumbImagePath() : null);
                }
            }
            return Results.newSuccessResult(pageInfo.getList(), "查询成功", pageInfo.getTotal());
        }catch (Exception e){
            log.error("查询文件列表出错");
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "查询失败");
    }

    @Override
    public Result<GetFileDTO> getFileDetails(String fileId) {
        try {
            GetFileDTO file = fastdfsFileMapper.selectFileDetails(fileId);
            if(!EmptyUtils.fieldsIsEmpty(file)){
                return Results.newSuccessResult(file, "查询成功");
            }else{
                return Results.newFailResult(ErrorCode.DB_ERROR, "该文件不存在");
            }
        } catch (Exception e) {
            log.error("获取文件详情出错", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "查询失败");
    }

    @Override
    public Result<String> updateFileRemark(String fileId, String description) {
        try {
            SysFastdfsFile file = new SysFastdfsFile();
            file.setDescription(description).setId(fileId);
            if(fastdfsFileMapper.updateById(file) == 1){
                return Results.newSuccessResult(fileId, "更新成功");
            }
        } catch (Exception e) {
            log.error("更新文件备注信息出错", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "更新失败");
    }
}
